$(
    function () {
        $("#username").keyup(

            function(){
                checkUserName();
            }
        );

        $("#password").keyup(
            function () {
                checkPassword();
            }
        );
        $("#checkPass").keyup(
            function () {
                checkConfirmPassword();
            }
        );
        $("#email").on("keyup click blur",function () {
            checkEmail();
        });

        $("#weight").keyup(
            function () {
                checkWeight();
            }
        );
        $("#mobile").keyup(
            function () {
                checkMobile();
            }
        )

    }
)

function checkUserName() {
    let obj = $("#username");
    let reg = /^[a-zA-Z]\w{5,15}$/;
    return myreg(reg,obj)
    //
}
function checkPassword() {
    let obj = $("#password");
    let reg = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
    return myreg(reg,obj)

}
function checkConfirmPassword() {
    let checkPass = $("#checkPass").val();
    let password = $("#password").val();
    if(checkPass!=""&&checkPass==password){
        $("#checkPass").parent().next().removeClass();
        $("#checkPass").parent().next().addClass("success-tip");
        return true;
    }
    else{
        console.log(password);
        $("#checkPass").parent().next().removeClass();
        $("#checkPass").parent().next().addClass("error-tip");
        return false;
    }
}

function checkEmail() {
    let obj = $("#email");
    let reg =  /(\S)+[@]{1}(\S)+[.]{1}(\w)+/;
    return myreg(reg,obj)

}


function checkWeight() {

    let obj = $("#weight");
    let reg = /^(0(\.\d{1}){0,1}|[1-8]\d{1,3}(\.\d{1}){0,1}|9\d{1,2}(\.\d{1}){0,1}|999(\.0){0,1}|.{0})$/;
    return myreg(reg,obj);
}
function checkMobile() {
    let obj = $("#mobile");
    let reg = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
    return myreg(reg,obj);
}

function myreg(reg,obj) {
    if(reg.test(obj.val())){
        console.log("success");
        $(obj).parent().next().removeClass();
        $(obj).parent().next().addClass("success-tip");
        return true;
    }
    else{
        console.log("error");
        $(obj).parent().next().removeClass();
        $(obj).parent().next().addClass("error-tip");
        return false;
    }


}

function register() {


    if(checkUserName()&&checkPassword()&&checkConfirmPassword()&&checkWeight()&&checkMobile()&&checkEmail()){
        let data={
            username:$("#username").val(),
            password:$("#password").val(),
            weight:$("#weight").val(),
            mobile:$("#mobile").val(),
            email:$("#email").val(),
            gender:$("input:radio[name=gender]:checked").val(),
            hobby:getCheckBox("hobby").toString(),
            birthday:$("#birthday").val()
        };
        console.log(data);
        $.ajax({
            url:'/register',
            data:data,
            type:'post',
            dataType:'json',
            success(data){
                console.log(data);
            }
        })


    }


}

function login() {
    
}

function getCheckBox(name) {
    let vals = [];
    $('input:checkbox[name='+name+']:checked').each(function () {
            vals.push($(this).val());


    })
    return vals;

}